package com.rookieci.totp.utils;

import android.net.Uri;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rookieci.totp.mode.TotpMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 磁磁帅 on 2025/9/18 0018.
 * 功能：
 */
public class TotpUtils {
    public static List<TotpMode> getModeOfJson(String codeStr) {
        if (TextUtils.isEmpty(codeStr)) return null;
        String json = codeStr.trim();
        List<TotpMode> modeList = new ArrayList<>();
        if (json.startsWith("[") || json.startsWith("]")) {
            try {
                List<TotpMode> o = new Gson().fromJson(json, new TypeToken<List<TotpMode>>() {
                }.getType());
                if (o != null && o.size() > 0) modeList.addAll(o);
            } catch (Exception e) {
                //失败就不管了
            }
        } else if (json.startsWith("{") || json.startsWith("}")) {
            try {
                TotpMode o = new Gson().fromJson(json, TotpMode.class);
                if (o != null) modeList.add(o);
            } catch (Exception e) {
                //失败就不管了
            }
        }
        return modeList;
    }

    public static TotpMode getModeOfScheme(String codeStr) {
        try {
            Uri uri = Uri.parse(codeStr);
            String scheme = uri.getScheme();
            if (!TextUtils.isEmpty(scheme) && "otpauth".equalsIgnoreCase(scheme)) {

            }
        } catch (Exception e) {
        }
        return null;
    }
}
